/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dataoke.shoppingguide.app773026.bean.RankingBean;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class SearchHotListAdapter extends RecyclerView.Adapter<SearchHotListAdapter.ViewHolder> {

    private List<RankingBean> mList = new ArrayList<>();
    private Context context;

    private OnClickListener onClickListener;

    private OnItemClickListener onItemClickListener;

    public SearchHotListAdapter(Context context) {
        this.context = context;
        initListener();
    }

    public SearchHotListAdapter(Context context, List<RankingBean> list) {
        this(context);
        this.mList = list;
    }
    public interface OnItemClickListener {
        void onItemClick(int position, RankingBean rankingBean);
    }
    /**
     * 可以共用同一个listener，相对高效
     */
    public static abstract class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
            onClick(holder.getAdapterPosition());
        }

        public abstract void onClick(int position);
    }

    /**
     * 初始化listener
     */
    private void initListener() {
        onClickListener = new OnClickListener() {
            @Override
            public void onClick(int position) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(position, mList.get(position));
            }
        };

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_hot, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RankingBean contentBean = mList.get(position);

        holder.mKeyWord.setText(contentBean.getKeyword());
        GlideUtils.loadImgCrop(context, contentBean.getItemPic(), holder.mPic);
        if(position == 0) {
            holder.mIvOrder.setImageDrawable(context.getResources().getDrawable(R.mipmap.top1));
        } else if (position == 1) {
            holder.mIvOrder.setImageDrawable(context.getResources().getDrawable(R.mipmap.top2));
        } else if (position == 2) {
            holder.mIvOrder.setImageDrawable(context.getResources().getDrawable(R.mipmap.top3));
        }
        if(position%3==0) {
            holder.mIvKey.setImageDrawable(context.getResources().getDrawable(R.mipmap.bao));
        } else {
            holder.mIvKey.setImageDrawable(context.getResources().getDrawable(R.mipmap.re));
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView mKeyWord;
        ImageView mIvKey;
        ImageView mPic;
        ImageView mIvOrder;
        LinearLayout mLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mKeyWord = itemView.findViewById(R.id.tv_keyword);
            mIvKey = itemView.findViewById(R.id.iv_key);
            mPic = itemView.findViewById(R.id.iv_pic);
            mIvOrder = itemView.findViewById(R.id.iv_order);

            mLayout = itemView.findViewById(R.id.cl_product_item);
            mLayout.setTag(this);
            mLayout.setOnClickListener(onClickListener);
        }
    }
    public void setData(List<RankingBean> contentBean) {
        mList.clear();
        mList.addAll(contentBean);
        notifyDataSetChanged();
    }

}
